<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>假数据生成</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
    <script src="js/jquery.min.js"></script>
</head>
<body>
<div style="margin-top:100px;"></div>
<div class="layui-container">
    <div>
        <button class="layui-btn layui-btn-normal" onclick="refreshConfig()">手动刷新配置</button>
    </div>
    <fieldset class="layui-elem-field" style="margin-top:20px;">
        <legend>单表随机数据生成</legend>
        <div class="layui-field-box" style="padding:30px 20px;">
            <form class="layui-form" onsubmit="return false">
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <input type="text" name="table" required lay-verify="required" placeholder="请输入表名"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="times" required lay-verify="required" placeholder="请输入生成数据条数"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux g1"></div>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="singleGenerate">生成</button>
                </div>
            </form>
        </div>
    </fieldset>
    <fieldset class="layui-elem-field">
        <legend>多表随机数据生成</legend>
        <div class="layui-field-box" style="padding:30px 20px;">
            <form class="layui-form" action="" id="multiply">
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <input type="text" name="times" required placeholder="标准生成条数"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">如果子项配置为空则取当前数据</div>
                    <div class="layui-form-mid layui-word-aux g2"></div>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="multiplyGenerate">生成</button>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <input type="text" name="table1" required placeholder="请输入表名"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="times1" required placeholder="请输入生成数据条数"
                               autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-normal" onclick="addComponent()">
                        <i class="layui-icon">&#xe608;</i>
                    </button>
                </div>
            </form>
        </div>
    </fieldset>
    <fieldset class="layui-elem-field">
        <legend>生成所有表随机数据</legend>
        <div class="layui-field-box" style="padding:30px 20px;">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <input type="text" name="times" required lay-verify="required" placeholder="请输入生成数据条数"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux g3"></div>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="allGenerate">生成</button>
                </div>
            </form>
        </div>
    </fieldset>

    <fieldset class="layui-elem-field">
        <legend>关联表随机数据生成</legend>
        <div class="layui-field-box" style="padding:30px 20px;">
            <form class="layui-form" onsubmit="return false">
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <input type="text" name="table" required lay-verify="required" placeholder="请输入主表名"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="times" required lay-verify="required" placeholder="请输入生成数据条数"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux g4"></div>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="dependentGenerate">生成</button>
                </div>
            </form>
        </div>
    </fieldset>


<!--    <fieldset class="layui-elem-field" style="margin-top:60px;">-->
<!--        <legend>表配置</legend>-->
<!--        <fieldset class="layui-elem-field layui-field-title" style="margin-bottom:0">-->
<!--            <legend>account表</legend>-->
<!--            <form class="layui-form" action="" style="margin-top:20px;">-->
<!--                <div class="layui-collapse" style="border-width:0">-->
<!--                    <div class="layui-colla-item">-->
<!--                        <h2 class="layui-colla-title">-->
<!--                            money-->
<!--                        </h2>-->
<!--                        <div class="layui-colla-content layui-show">-->
<!--                            <div class="layui-form-item">-->
<!--                                <label class="layui-form-label">配置类型</label>-->
<!--                                <div class="layui-input-block">-->
<!--                                    <input type="radio" name="type" value="常规类型" title="常规类型">-->
<!--                                    <input type="radio" name="type" value="字符模板" title="字符模板" checked>-->
<!--                                    <input type="radio" name="type" value="常用数据类型" title="常用数据类型">-->
<!--                                    <input type="radio" name="type" value="自定义类型" title="自定义类型">-->
<!--                                </div>-->
<!--                            </div>-->
<!--                            <div class="layui-form-item">-->
<!--                                <label class="layui-form-label">最大值</label>-->
<!--                                <div class="layui-input-inline">-->
<!--                                    <input type="text" name="max" placeholder="请输入最大值" autocomplete="off"-->
<!--                                           class="layui-input">-->
<!--                                </div>-->
<!--                                <label class="layui-form-label">最小值</label>-->
<!--                                <div class="layui-input-inline">-->
<!--                                    <input type="text" name="min" placeholder="请输入最小值" autocomplete="off"-->
<!--                                           class="layui-input">-->
<!--                                </div>-->
<!--                            </div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </form>-->
<!--        </fieldset>-->
<!--    </fieldset>-->
    <div style="height:200px;"></div>
</div>

<script>
    var index = 1;
    // 是否允许点击
    var g1 = true, g2 = true, g3 = true,g4 = true,g5 = true;
    $(function () {
        //Demo
        layui.use(['form', 'element'], function () {
            var form = layui.form;
            var element = layui.element;

            //单表
            form.on('submit(singleGenerate)', function (data) {
                if (!g1) {
                    return false;
                }
                $(".g1").text("数据生成中");
                g1 = false;
                $.ajax({
                    url: "/system/generateData",
                    method: "get",
                    data: data.field,
                    success: function (e) {
                        if (e) {
                            if (e.code === "200") {
                                layer.msg("生成成功");
                            } else {
                                layer.msg(e.message);
                            }
                        } else {
                            layer.msg("请求失败");
                        }
                    }, error() {
                        layer.msg("请求失败");
                    },
                    complete() {
                        g1 = true;
                        $(".g1").text("执行完毕");
                    }
                });
                return false;
            });

            //多表
            form.on('submit(multiplyGenerate)', function (data) {
                if (!g2) {
                    return false;
                }
                $(".g2").text("数据生成中");
                var fields = data.field;
                var dataPram = {};
                var list = [];
                dataPram.times = fields.times;
                for (var i = 0; i < index; i++) {
                    if (fields['table' + (i + 1)] != null) {
                        list.push({table: fields['table' + (i + 1)], times: fields['times' + (i + 1)]});
                    }
                }
                dataPram.tableList = list;
                g2 = false;
                $.ajax({
                    url: "/system/insertMultiplyTableData",
                    method: "post",
                    data: JSON.stringify(dataPram),
                    contentType: "application/json",
                    dataType: "json",
                    success: function (e) {
                        if (e) {
                            if (e.code === "200") {
                                layer.msg("生成成功");
                            } else {
                                layer.msg(e.message);
                            }
                        } else {
                            layer.msg("请求失败");
                        }
                    }, error() {
                        layer.msg("请求失败");
                    },
                    complete() {
                        g2 = true;
                        $(".g2").text("执行完毕");
                    }
                });
                return false;
            });

            //所有表
            form.on('submit(allGenerate)', function (data) {
                if (!g3) {
                    return false;
                }
                $(".g3").text("数据生成中");
                g2 = false;
                $.ajax({
                    url: "/system/insertAllTableData",
                    method: "get",
                    data: data.field,
                    success: function (e) {
                        if (e) {
                            if (e.code === "200") {
                                layer.msg("生成成功");
                            } else {
                                layer.msg(e.message);
                            }
                        } else {
                            layer.msg("请求失败");
                        }
                    }, error() {
                        layer.msg("请求失败");
                    },
                    complete() {
                        g3 = true;
                        $(".g3").text("执行完毕");
                    }
                });
                return false;
            });


            //单表
            form.on('submit(dependentGenerate)', function (data) {
                if (!g4) {
                    return false;
                }
                $(".g4").text("数据生成中");
                g4 = false;
                $.ajax({
                    url: "/system/dependentGenerate",
                    method: "get",
                    data: data.field,
                    success: function (e) {
                        if (e) {
                            if (e.code === "200") {
                                layer.msg("生成成功");
                            } else {
                                layer.msg(e.message);
                            }
                        } else {
                            layer.msg("请求失败");
                        }
                    }, error() {
                        layer.msg("请求失败");
                    },
                    complete() {
                        g4 = true;
                        $(".g4").text("执行完毕");
                    }
                });
                return false;
            });
        });
    });

    function addComponent() {
        index++;
        $("#multiply").append('<div class="layui-form-item"><div class="layui-input-inline"><input type="text"name="table' + index + '"required lay-verify="required"placeholder="请输入表名"autocomplete="off"class="layui-input"></div><div class="layui-input-inline"><input type="text"name="times' + index + '"required lay-verify="required"placeholder="请输入生成数据条数"autocomplete="off"class="layui-input"></div><button class="layui-btn layui-btn-normal" onclick="addComponent()"><i class="layui-icon">&#xe608;</i></button></div>');
    }

    function refreshConfig(){
        if (!g5) {
            return false;
        }
        g5 = false;
        $.ajax({
            url: "/system/refreshConfig",
            method: "get",
            success: function (e) {
                if (e) {
                    if (e.code === "200") {
                        layer.msg("刷新成功");
                    } else {
                        layer.msg(e.message);
                    }
                } else {
                    layer.msg("请求失败");
                }
            }, error() {
                layer.msg("请求失败");
            },
            complete() {
                g5 = true;
            }
        });
    }
</script>
</body>
</html>